#!/bin/bash

usage() {
    echo "Runs querybook tests"
    echo ''
    echo 'If not specified, then both python and node tests'
    echo 'will be executed.'
    echo ''
    echo 'Flags:'
    echo '  --python    only run python tests'
    echo '  --node      only run node js tests'
    echo
    echo 'Usage:'
    echo '  $0 [--python]'
    exit 1
}

run_python_unit_test() {
    echo 'Start running python unit tests >>>>>>>>>>>>>>>>>>>>>>>>>>>>'
    python -m gevent.monkey --module pytest querybook/tests || exit 1
}

run_webpack_test() {
    echo 'Start running webpack >>>>>>>>>>>>>>>>>>>>>>>>>>>>'
    ./node_modules/.bin/webpack --mode=production
    if [ $? -ne 0 ]; then
        echo "Webpack test failed"
        exit 1
    fi
}

run_js_unit_test() {
    echo 'Start running js unit tests >>>>>>>>>>>>>>>>>>>>>>>>>>>>'
    NODE_ENV=test ./node_modules/.bin/jest
}

run_ts_validation() {
    echo 'Start running ts validation  >>>>>>>>>>>>>>>>>>>>>>>>>>>>'
    npm run tsc-check
}

run_eslint_validation() {
    echo 'Start running eslint validation  >>>>>>>>>>>>>>>>>>>>>>>>>>>>'
    npm run lint
}

PYTHON_TESTS=(
    "run_python_unit_test"
)

NODE_TESTS=(
    "run_ts_validation"
    "run_js_unit_test"
    "run_eslint_validation"
    "run_webpack_test"
)

tests=("${PYTHON_TESTS[@]}" "${NODE_TESTS[@]}")
while [[ "$#" -gt 0 ]]; do
  case $1 in
    --python) tests=("${PYTHON_TESTS[@]}");;
    --node) tests=("${NODE_TESTS[@]}");;
    --help) usage;;
      *) echo "Unknown parameter passed: $1"; usage;;
  esac;
  shift;
done

children_pids=()

clen=`expr "${#tests[@]}" - 1`

for i in `seq 0 "$clen"`; do
    ${tests[$i]} &
    children_pids+=("$!")
done

wait_and_get_exit_codes() {
    children=("$@")
    EXIT_CODE=0
    for job in "${children[@]}"; do
       CODE=0;
       wait ${job} || CODE=$?
       if [[ "${CODE}" != "0" ]]; then
            EXIT_CODE=1
       fi
   done
}

wait_and_get_exit_codes "${children_pids[@]}"

exit "$EXIT_CODE"
